﻿using ScratchNet.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace ScratchNet
{
    public class EDSIFStatement : Statement
    {
        public EDSIFStatement()
        {
            Consequent = new BlockStatement();
        }
        public Expression Test { get; set; }
        public BlockStatement Consequent
        {
            get;
            set;
        }
        BlockStatement _alternate;

        public BlockStatement Alternate
        {
            get
            {
                return _alternate;
            }
            set
            {
                _alternate = value;
            }
        }

        public override string ReturnType
        {
            get { return "void"; }
        }
        

        protected override Completion ExecuteImpl(ExecutionEnvironment enviroment)
        {
            if (Test == null)
                return Completion.Exception(Language.NullException, this);
            var t = Test.Execute(enviroment);
            if (t.Type != CompletionType.Value)
                return t;
            if (t.ReturnValue is bool)
            {
                //都要解析成语句
                string trueRes = string.Empty;
                string falseRes = string.Empty;

                if (Consequent != null)
                {
                    ExecutionEnvironment current = new ExecutionEnvironment(enviroment);
                    var c = Consequent.Execute(current);
                    if (c.IsException)
                    {
                        return c;
                    }
                    trueRes = Consequent.Execute(current).ExecStatementStr ?? "{}";
                }

                if (Alternate != null)
                {
                    ExecutionEnvironment current = new ExecutionEnvironment(enviroment);
                    var c = Alternate.Execute(current);
                    if (c.IsException)
                    {
                        return c;
                    }
                    falseRes = c.ExecStatementStr ?? "{}";
                }

                string formattedString = string.Format("{{ if({0}){1}else {{{2}}} }}", t.ExecStatementStr, trueRes, falseRes);
                return new Completion(null, formattedString);
            }
            else
                return Completion.Exception(Properties.Language.NotBoolean, Test);
        }

        public override Descriptor Descriptor
        {
            get
            {
                Descriptor desc = new Descriptor();
                desc.Add(new TextItemDescriptor(this, "if ", true));
                desc.Add(new TextItemDescriptor(this, "("));
                desc.Add(new ExpressionDescriptor(this, "Test", "boolean") { IsOnlyNumberAllowed = false });
                desc.Add(new TextItemDescriptor(this, ")"));
                return desc;
            }
        }
        public override BlockDescriptor BlockDescriptor
        {
            get
            {
                BlockDescriptor block = new BlockDescriptor();
                block.Add(new BlockStatementDescriptor(this, "Consequent"));
                if (Alternate != null)
                {
                    Descriptor d = new Descriptor();
                    d.Add(new TextItemDescriptor(this, "else", true));
                    block.Add(new ExpressionStatementDescription(this, "else", d));
                    block.Add(new BlockStatementDescriptor(this, "Alternate"));
                }
                return block;
            }
        }
        public override string Type
        {
            get
            {
                return "EDSIfStatement";
            }
        }
        public override bool IsClosing
        {
            get { return false; }
        }

    }
}
